We should describe an entity as an interface when we identify its type based on its behaviour rather than its properties.
An interface is same as class. it include variables and methods, but by default, the methods declared in an interface are abstract it means just method signature, no body.
Before the interface method, the Java compiler adds the keywords public and abstract.
Before data members, the compiler also adds the public, static, and final keywords.
By default, interface fields are public, static, and final and interface methods are public and abstract .
Syntax
interface{
// declare constant fields
// declare methods that abstract
// by default.
}
// Java Interface Example
interface Displayable { void display(); } class Test implements Displayable { public void display(){ System.out.println("Hello"); } } class Main{ public static void main(String[] args){ Test test = new Test(); test.display(); } }
Output:
Hello
In below example Shape Interface has only one method draw but its implementation is provided by circle and triangle classes.It means interface is defined by someone else, but its implementation is provided by some other people and used by someone else but implementation part is hidden by the user who uses the interface.
interface Shape { void Draw(); } class Triangle implements Shape { public void Draw(){ System.out.println("Drawing Triangle......"); } } class Circle implements Shape { public void Draw(){ System.out.println("Drawing Circle......"); } } class Main{ public static void main(String[] args){ Shape shape = new Triangle(); shape.Draw(); Shape shape2 = new Circle(); shape2.Draw(); } }
Output:
Drawing Triangle
Drawing Circle
Due to ambiguity, multiple inheritance is not supported in the case of classes. However, since there is no ambiguity in the case of an interface, it is supported.
Multiple inheritance occurs when a class implements multiple interfaces or when an interface extends multiple interfaces.
interface Shape { void Draw(); } interface Displayable{ void Display(); } class Circle implements Shape,Displayable { // multiple inheritance @Override public void Draw() { // TODO Auto-generated method stub System.out.println("Drawing Circle......"); } @Override public void Display() { // TODO Auto-generated method stub System.out.println("Display Circle."); } } class Main{ public static void main(String[] args){ Circle circle = new Circle(); circle.Draw(); circle.Display(); } }
Output:
Drawing Circle......
Display Circle.
interface Displayable{ void Display(); } interface Shape extends Displayable { void Draw(); } class Circle implements Shape{ @Override public void Draw() { // TODO Auto-generated method stub System.out.println("Drawing Circle......"); } @Override public void Display() { // TODO Auto-generated method stub System.out.println("Display Circle"); } } class Main{ public static void main(String[] args){ Circle circle = new Circle(); circle.Draw(); circle.Display(); } }
Output:
Drawing Circle......
Display Circle
Since java 8 static methods are defined in interfaces, they can be called without an object. however, these method are not inherited.
interface Bike{ void MaxSpeed(); static void StaticCheck(){ // Static method in interface System.out.println("Static method from interface "); } } class Hero implements Bike{ public void MaxSpeed() { System.out.println("Max Speed is : 140 kmph"); } } class Main{ public static void main(String[] args) { Hero hero=new Hero(); hero.MaxSpeed(); Bike.StaticCheck(); // static method called without object creation } }
Output:
Max Speed is : 140 kmph
Static method from interface
Since Java 8, method body can be define in interfaces. However, we must make it default method with default keyword.
interface Bike{ void MaxSpeed(); default void DefaultCheck(){ System.out.println("Default method from interface"); } } class Hero implements Bike{ public void MaxSpeed() { System.out.println("Max Speed is : 140 kmph"); } } class Main{ public static void main(String[] args) { Bike bike=new Hero(); bike.MaxSpeed(); bike.DefaultCheck(); } }
Output:
Max Speed is : 140 kmph
Default method from interface
An interface that contains neither methods nor constants is referred to as a marker interface. The compiler and JVM can learn more about the object because it provides run-time type information.
Marker interface is also called tagging interface.
Syntax
public interface Deletable {
}
Difference Between Class and Interface are mention below
S.N | Class | Interface |
---|---|---|
1. | we can instantiate variables and make objects in a class. | we cannot instantiate variables and make an object in an interface. |
2. | Classes may include methods with implementation | The implementation of methods is not permitted in the interface. |
3. | Classes can use the private, protected, and public access specifiers. | Public is the only specifier used in Interface. |
Post your comment